package com.xiao.xin.comfyui.client.handler;

import com.alibaba.fastjson2.JSON;
import com.xiao.xin.core.domain.MessageBase;
import com.xiao.xin.core.service.ComfyuiMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/***
 * 配置客户端处理器详细
 * 拦截器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ComfyuiMessageHandler extends TextWebSocketHandler {

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("========== 连接成功");
    }

    private final ComfyuiMessageService comfyuiMessageService;

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("收到消息:{}",JSON.toJSONString(message.getPayload()));
        MessageBase messageBase = JSON.parseObject(message.getPayload(), MessageBase.class);
        comfyuiMessageService.handleMessage(messageBase);
    }
}
